class VideoController < ApplicationController

  before_filter :set_ref, :only => [:add_video,:create_video, :edit_video , :delete_video , :update_video ,:show_video] 
  before_filter :authorize_ref_owner, :only => [:add_video,:create_video, :edit_video , :delete_video , :update_video ,:show_video]

  def add_video
    render :partial => '/video/new'
  end
  
  def edit_video
    render :partial => '/video/edit'
  end 
  
  def delete_video
    if !@ref.update_attribute("video_link", nil)
      flash[:notice] = 'Video was not updated.'
    end
    render :partial => '/ecases/video_or_slideshow_admin'
  end 
  
  def show_video
    render :partial => '/ecases/video_or_slideshow_admin'
  end
  
  def create_video
    vl = params[:video_link]
    vl = trasform_video(vl)
    if !@ref.update_attribute("video_link", vl)
      flash[:notice] = 'Video was not updated.'
    end
    render :partial => '/video/video_admin'
  end
  
  def update_video
    vl = params[:video_link]
    vl = trasform_video(vl)
    if !@ref.update_attribute("video_link", vl)
      flash[:notice] = 'Video was not updated.'
    end
    render :partial => '/video/video_admin'
  end
  
  private   
  
  def set_ref
    if(params[:v_type]=="ecase")
      @ref = Ecase.find(params[:id])
    else
      @ref= Event.find(params[:id])
    end
  end  
  
  def trasform_video v
    v.gsub(/(width=")[0-9]*(")/) {|s| 'width="390"'  }
    v.gsub(/(height=")[0-9]*(")/) {|s| 'height="321"'  }
  end 
    
  def authorize_ref_owner
    return false unless authorize_author

    authorized = admin_authorized? || (get_session_user.id == @ref.author_id) 

    unless authorized
      on_not_authorized("Please login as author of this ecase.")
    end

    authorized
  end
end
